﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;
using RibbonEditor.AppCode;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class ComboboxControl : UserControl, IControlRulable
    {
        private CommandUIDefinitionClasses.ComboBox innerCombobox;

        public CommandUIDefinitionClasses.ComboBox InnerCombobox
        {
            get { return innerCombobox; }
            set { innerCombobox = value; }
        }

        public ComboboxControl()
        {
            InitializeComponent();
            this.innerCombobox = new CommandUIDefinitionClasses.ComboBox();
            this.FillControls();
        }

        public ComboboxControl(CommandUIDefinitionClasses.ComboBox cBox)
            : this()
        {
            this.innerCombobox = cBox;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.bcAllowFreeForm.Value = this.innerCombobox.AllowFreeForm;
                this.rcAlt.Value = this.innerCombobox.Alt;
                this.rcAltArrow.Value = this.innerCombobox.AltArrow;
                this.bcAutoComplete.Value = BoolHelper.GetBoolValue(this.innerCombobox.AutoComplete);
                this.icAutoCompleteDelay.Value = this.innerCombobox.AutoCompleteDelay;
                this.bcCacheMenuVersions.Value = BoolHelper.GetBoolValue(this.innerCombobox.CacheMenuVersions);
                this.rcCommand.Value = this.innerCombobox.Command;
                this.rcCommandMenuClose.Value = this.innerCombobox.CommandMenuClose;
                this.rcCommandMenuOpen.Value = this.innerCombobox.CommandMenuOpen;
                this.rcCommandPreview.Value = this.innerCombobox.CommandPreview;
                this.rcCommandPreviewRevert.Value = this.innerCombobox.CommandPreviewRevert;
                this.strId.Value = this.innerCombobox.Id;
                this.bcImeEnabled.Value = BoolHelper.GetBoolValue(this.innerCombobox.ImeEnabled);
                this.strInitialItem.Value = this.innerCombobox.InitialItem;
                this.bcPopulateDynamically.Value = BoolHelper.GetBoolValue(this.innerCombobox.PopulateDynamically);
                this.bcPopulateOnlyOnce.Value = BoolHelper.GetBoolValue(this.innerCombobox.PopulateOnlyOnce);
                this.strPopulateQueryCommand.Value = this.innerCombobox.PopulateQueryCommand;
                this.strQueryCommand.Value = this.innerCombobox.QueryCommand;
                this.icSequence.Value = this.innerCombobox.Sequence;
                this.strTemplateAlias.Value = this.innerCombobox.TemplateAlias;
                this.rcToolTipDescription.Value = this.innerCombobox.ToolTipDescription;
                this.strToolTipKeyWord.Value = this.innerCombobox.ToolTipHelpKeyWord;
                this.rcToolTipImage32by32.Value = this.innerCombobox.ToolTipImage32by32;
                this.strToolTipImage32by32Class.Value = this.innerCombobox.ToolTipImage32by32Class;
                this.icToolTipImage32by32Left.Value = this.innerCombobox.ToolTipImage32by32Left;
                this.icToolTipImage32by32Top.Value = this.innerCombobox.ToolTipImage32by32Top;
                this.strToolTipShortcutKey.Value = this.innerCombobox.ToolTipShortcutKey;
                this.rcToolTipTitle.Value = this.innerCombobox.ToolTipTitle;
                this.strWidth.Value = this.innerCombobox.Width;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerCombobox.AllowFreeForm = this.bcAllowFreeForm.Value;
                    this.innerCombobox.Alt = this.rcAlt.Value;
                    this.innerCombobox.AltArrow = this.rcAltArrow.Value;
                    this.innerCombobox.AutoComplete = this.bcAutoComplete.Value;
                    this.innerCombobox.AutoCompleteDelay = this.icAutoCompleteDelay.Value;
                    this.innerCombobox.CacheMenuVersions = this.bcCacheMenuVersions.Value;
                    this.innerCombobox.Command = this.rcCommand.Value;
                    this.innerCombobox.CommandMenuClose = this.rcCommandMenuClose.Value;
                    this.innerCombobox.CommandMenuOpen = this.rcCommandMenuOpen.Value;
                    this.innerCombobox.CommandPreview = this.rcCommandPreview.Value;
                    this.innerCombobox.CommandPreviewRevert = this.rcCommandPreviewRevert.Value;
                    this.innerCombobox.Id = this.strId.Value;
                    this.innerCombobox.ImeEnabled = this.bcImeEnabled.Value;
                    this.innerCombobox.InitialItem = this.strInitialItem.Value;
                    this.innerCombobox.PopulateDynamically = this.bcPopulateDynamically.Value;
                    this.innerCombobox.PopulateOnlyOnce = this.bcPopulateOnlyOnce.Value;
                    this.innerCombobox.PopulateQueryCommand = this.strPopulateQueryCommand.Value;
                    this.innerCombobox.QueryCommand = this.strQueryCommand.Value;
                    this.innerCombobox.Sequence = this.icSequence.Value;
                    this.innerCombobox.TemplateAlias = this.strTemplateAlias.Value;
                    this.innerCombobox.ToolTipDescription = this.rcToolTipDescription.Value;
                    this.innerCombobox.ToolTipHelpKeyWord = this.strToolTipKeyWord.Value;
                    this.innerCombobox.ToolTipImage32by32 = this.rcToolTipImage32by32.Value;
                    this.innerCombobox.ToolTipImage32by32Class = this.strToolTipImage32by32Class.Value;
                    this.innerCombobox.ToolTipImage32by32Left = this.icToolTipImage32by32Left.Value;
                    this.innerCombobox.ToolTipImage32by32Top = this.icToolTipImage32by32Top.Value;
                    this.innerCombobox.ToolTipShortcutKey = this.strToolTipShortcutKey.Value;
                    this.innerCombobox.ToolTipTitle = this.rcToolTipTitle.Value;
                    this.innerCombobox.Width = this.strWidth.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ComboboxControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerCombobox.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
